package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.SysRoleDto;
import com.itheima.ydd.dto.SysUserDto;
import com.itheima.ydd.entity.SysRole;
import com.itheima.ydd.entity.SysUser;
import com.itheima.ydd.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/sys_user")
@Slf4j
@CrossOrigin
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    @GetMapping("/list")
    public R page(String status, String phone, int page, int limit) {
        Map map = new HashMap<>();
        Page<SysUser> pageInfo = new Page<>(page, limit);
        LambdaQueryWrapper<SysUser> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotEmpty(status), SysUser::getStatus, status);
        lqw.like(StringUtils.isNotEmpty(phone), SysUser::getPhone, phone);

        sysUserService.page(pageInfo, lqw);
        map.put("totalPages", pageInfo.getPages());
        map.put("currentPage", pageInfo.getCurrent());
        map.put("pageSize", pageInfo.getSize());
        map.put("count", pageInfo.getTotal());
        map.put("data", pageInfo.getRecords());

        return new R(map, "", 0);
    }


    @GetMapping("/del")
    public R delete(String id) {

        LambdaQueryWrapper<SysUser> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysUser::getId, id);
        sysUserService.remove(lqw);
        return R.success("", "");

    }



    @GetMapping("/rowInfo")
    public ResultData rowInfo(Integer id){
        SysUser sysUser = sysUserService.getById(id);
        /*SysUserDto dto=new SysUserDto();
        BeanUtils.copyProperties(sysUser,dto);
        LambdaQueryWrapper<SysRole>lqw=new LambdaQueryWrapper<>();
        lqw.eq(SysRole::getId,id);
        SysRole one = roleService.getOne(lqw);
        dto.setRoleId(one.getId());*/
        return ResultData.ok("",sysUser);
    }

    @PostMapping("/update")
    public R update(@RequestBody SysUserDto sysUserDto) {

        //修改密码
        if (StringUtils.isNotEmpty(sysUserDto.getPassword())) {
            String password = DigestUtils.md5DigestAsHex(sysUserDto.getPassword().getBytes());
            sysUserDto.setPassword(password);
        }else{
            //如果传回的密码为空,则修改密码为原密码
            SysUser user = sysUserService.getById(sysUserDto.getId());
            sysUserDto.setPassword(user.getPassword());
        }

        //修改roleId
        if (sysUserDto.getRoleIds() != null) {
            List<String> roleIds = sysUserDto.getRoleIds();

            //使roleId符合数据库对应格式
            StringBuilder roleId = new StringBuilder("[");
            for (int i = 0; i < roleIds.size(); i++) {
                roleId.append(i != roleIds.size() - 1 ? "\"" + roleIds.get(i) + "\"," : "\"" + roleIds.get(i) + "\"]");
            }

            sysUserDto.setRoleId(roleId.toString());
        }

        sysUserService.updateById(sysUserDto);

        return R.success("","");
    }



    @PostMapping("/add")
    public R save(@RequestBody SysUserDto sysUserDto) {

        //设置id
        String id = UUID.randomUUID().toString();
        sysUserDto.setId(id);

        //设置mmd5密码
        String password = DigestUtils.md5DigestAsHex(sysUserDto.getPassword().getBytes());
        sysUserDto.setPassword(password);

        //设置roleId对应数据库格式
        List<String> roleIds = sysUserDto.getRoleIds();

        String roleId = "[";

        for (int i = 0; i < roleIds.size(); i++) {
            roleId += i != roleIds.size() - 1 ? "\"" + roleIds.get(i) + "\"," : "\"" + roleIds.get(i) + "\"]";
        }

        sysUserDto.setRoleId(roleId);

        sysUserService.save(sysUserDto);

        return R.success("","");

    }


}


